package www.wen.com.common.lib.util

import android.annotation.SuppressLint
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author WEN
 * @Description:
 * @date 2021/3/2 0:00
 */
object DateUtil {

    private lateinit var calendar: Calendar
    const val FORMAT_NORMAL = "yyyy-MM-dd HH:mm:ss"

    /**
     * 功能描述：返回日
     */
    fun getDay(millisecond: Long): Int {
        calendar = Calendar.getInstance()
        calendar.time = Date(millisecond)
        return calendar.get(Calendar.DAY_OF_MONTH)
    }

    /**
     * 功能描述：返回年
     */
    fun getYear(millisecond: Long): Int {
        calendar = Calendar.getInstance()
        calendar.time = Date(millisecond)
        return calendar.get(Calendar.YEAR)
    }

    /**
     * 功能描述：返回年
     */
    fun getMonth(millisecond: Long): Int {
        calendar = Calendar.getInstance()
        calendar.time = Date(millisecond)
        return calendar.get(Calendar.MONTH)
    }

    /**
     * 功能描述：返回英文月份
     */
    fun getMonthFormatEn(millisecond: Long): String {
        calendar = Calendar.getInstance()
        calendar.time = Date(millisecond)
        var month = ""
        when (calendar.get(Calendar.MONTH)) {
            0 -> month = "Jan"
            1 -> month = "Feb"
            2 -> month = "Mar"
            3 -> month = "Apr"
            4 -> month = "May"
            5 -> month = "Jun"
            6 -> month = "Jul"
            7 -> month = "Aug"
            8 -> month = "Sep"
            9 -> month = "Oct"
            10 -> month = "Nov"
            11 -> month = "Dec"
        }
        return month
    }

    /**
     * 功能描述：返回数字月份
     */
    fun getMonthNumber(millisecond: Long): Int {
        calendar = Calendar.getInstance()
        calendar.time = Date(millisecond)
        return calendar.get(Calendar.MONTH) + 1
    }

    fun format(timeStamp: Long, format: String = "yyyy-MM-dd HH:mm"): String {
        val simpleDateFormat = SimpleDateFormat(format)
        return simpleDateFormat.format(Date(timeStamp))
    }

    /**
     * 把已经格式化的时间字符串还原成时间戳
     */
    @SuppressLint("SimpleDateFormat")
    fun strToTimeStamp(timeStampStr: String, format: String = "yyyy-MM-dd"): Long {
        val simpleDateFormat = SimpleDateFormat(format)
        val date = simpleDateFormat.parse(timeStampStr)
        return date!!.time
    }

    /**
     * 获取当日零点零分零秒的时间戳
     */
    fun getZero(): Long {
        val current = System.currentTimeMillis()
        return current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().rawOffset
    }

    /**
     * 获取当日零点零分零秒的时间戳(PDA时区)
     */
    fun getDefaultZero(): Long {
        val current = System.currentTimeMillis()
        return current / (1000 * 3600 * 24) * (1000 * 3600 * 24)
    }

    /**
     * 生成包括当日在内的一组时间戳
     * @param beforeDay 以前的天数
     * @param afterDay 未来的天数
     * @param currentZeroTimeStamp 填当日零点时间戳
     */
    fun provideTimestampList(currentZeroTimeStamp: Long = getZero(), beforeDay: Int = 3, afterDay: Int = 0): List<Long> {
        val oneDayTimestamp = 1000 * 60 * 60 * 24
        var startZeroTimeStamp = currentZeroTimeStamp - oneDayTimestamp * beforeDay
        val allDay = beforeDay + afterDay
        val timeStamps = mutableListOf<Long>()
        for (i in 0..allDay) {
            timeStamps.add(startZeroTimeStamp)
            startZeroTimeStamp += oneDayTimestamp
        }
        return timeStamps
    }

    /**
     * 格式化一组时间戳
     * @param timeList 时间戳
     * @return 已格式化的时间字符串
     */
    fun provideTimestampStringList(timeList: List<Long> = provideTimestampList(), format: String = "yyyy-MM-dd"): List<String> {
        val dataStrings = mutableListOf<String>()
        timeList.forEach {
            dataStrings.add(format(it, format))
        }
        return dataStrings
    }

}